package com.yunfeisoft.business.service.impl;

import com.applet.base.BaseServiceImpl;
import com.applet.sql.separation.DataSourceChange;
import com.applet.utils.Page;
import com.yunfeisoft.business.dao.inter.*;
import com.yunfeisoft.business.model.PmsProduct;
import com.yunfeisoft.business.model.PmsProductVertifyRecord;
import com.yunfeisoft.business.service.inter.PmsProductService;
import com.yunfeisoft.dao.inter.AttachmentDao;
import com.yunfeisoft.dao.inter.DataDao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * ClassName: PmsProductServiceImpl
 * Description: 商品表service实现
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Service("pmsProductService")
public class PmsProductServiceImpl extends BaseServiceImpl<PmsProduct, String, PmsProductDao> implements PmsProductService {

    @Autowired
    private DataDao dataDao;
    @Autowired
    private AttachmentDao attachmentDao;
    @Autowired
    private PmsProductLadderDao productLadderDao;
    @Autowired
    private PmsProductFullReductionDao productFullReductionDao;
    @Autowired
    private PmsSkuStockDao skuStockDao;
    @Autowired
    private PmsProductAttributeValueDao pmsProductAttributeValueDao;
    @Autowired
    private PmsProductVertifyRecordDao productVertifyRecordDao;

    @Override
    @DataSourceChange(slave = true)
    public Page<PmsProduct> queryPage(Map<String, Object> params) {
        return getDao().queryPage(params);
    }

    @Override
    public List<PmsProduct> queryList(Map<String, Object> params) {
        return getDao().queryList(params);
    }

    @Override
    public int modifyVerifyStatus(String id, int verifyStatus, String remark) {
        if (StringUtils.isBlank(id)) {
            return 0;
        }

        if (verifyStatus == PmsProduct.ProductVerifyStatusEnum.ACCEPT.getValue()) {
            PmsProductVertifyRecord vertifyRecord = new PmsProductVertifyRecord();
            vertifyRecord.setProductId(id);
            vertifyRecord.setStatus(verifyStatus);
            vertifyRecord.setDetail(StringUtils.isBlank(remark) ? "同意" : remark);
            productVertifyRecordDao.insert(vertifyRecord);
        }

        if (verifyStatus == PmsProduct.ProductVerifyStatusEnum.REJECT.getValue()) {
            PmsProductVertifyRecord vertifyRecord = new PmsProductVertifyRecord();
            vertifyRecord.setProductId(id);
            vertifyRecord.setStatus(verifyStatus);
            vertifyRecord.setDetail(remark);
            productVertifyRecordDao.insert(vertifyRecord);
        }

        PmsProduct product = new PmsProduct();
        product.setId(id);
        product.setVerifyStatus(verifyStatus);
        return getDao().update(product);
    }

    @Override
    public int modifyPublishStatus(String id, int publishStatus) {
        if (StringUtils.isBlank(id)) {
            return 0;
        }
        PmsProduct product = new PmsProduct();
        product.setId(id);
        product.setPublishStatus(publishStatus);
        return getDao().update(product);
    }

    @Override
    public int modifyNewStatus(String id, int newStatus) {
        if (StringUtils.isBlank(id)) {
            return 0;
        }
        PmsProduct product = new PmsProduct();
        product.setId(id);
        product.setNewStatus(newStatus);
        return getDao().update(product);
    }

    @Override
    public int modifyRecommandStatus(String id, int recommandStatus) {
        if (StringUtils.isBlank(id)) {
            return 0;
        }
        PmsProduct product = new PmsProduct();
        product.setId(id);
        product.setRecommandStatus(recommandStatus);
        return getDao().update(product);
    }

    @Override
    public List<PmsProduct> listAll(Map<String, Object> params) {
        return getDao().listAll(params);
    }

    /**
     * 根据品牌分页查询商品列表
     * @param params
     * @return
     */
    @Override
    public Page<PmsProduct> queryPageByBrand(Map<String, Object> params) {
        return getDao().queryPageByBrand(params);
    }

    /**
     * 查询最新的商品
     * @param params
     * @return
     */
    @Override
    public List<PmsProduct> queryNewProduct(Map<String, Object> params, int limit) {
        return getDao().queryNewProduct(params, limit);
    }

    @Override
    public int save(PmsProduct pmsProduct) {
        int result = super.save(pmsProduct);
        if (StringUtils.isNotBlank(pmsProduct.getContent())) {
            dataDao.saveOrUpdate(pmsProduct.getId(), pmsProduct.getContent());
        }

        if (ArrayUtils.isNotEmpty(pmsProduct.getImageIds())) {
            attachmentDao.updateRefId(pmsProduct.getImageIds(), pmsProduct.getId());
        }

        if (CollectionUtils.isNotEmpty(pmsProduct.getLadderList())) {
            productLadderDao.batchInsert(pmsProduct.getLadderList());
        }

        if (CollectionUtils.isNotEmpty(pmsProduct.getFullReductionList())) {
            productFullReductionDao.batchInsert(pmsProduct.getFullReductionList());
        }

        if (CollectionUtils.isNotEmpty(pmsProduct.getSkuStockList())) {
            skuStockDao.batchInsert(pmsProduct.getSkuStockList());
        }

        if (CollectionUtils.isNotEmpty(pmsProduct.getProductAttributeValueList())) {
            pmsProductAttributeValueDao.batchInsert(pmsProduct.getProductAttributeValueList());
        }

        return result;
    }

    @Override
    public int modify(PmsProduct pmsProduct) {
        dataDao.deleteByRefId(pmsProduct.getId());
        if (StringUtils.isNotBlank(pmsProduct.getContent())) {
            dataDao.saveOrUpdate(pmsProduct.getId(), pmsProduct.getContent());
        }

        attachmentDao.deleteRefId(new String[]{pmsProduct.getId()});
        if (ArrayUtils.isNotEmpty(pmsProduct.getImageIds())) {
            attachmentDao.updateRefId(pmsProduct.getImageIds(), pmsProduct.getId());
        }

        productLadderDao.deleteByProductId(pmsProduct.getId());
        if (CollectionUtils.isNotEmpty(pmsProduct.getLadderList())) {
            productLadderDao.batchInsert(pmsProduct.getLadderList());
        }

        productFullReductionDao.deleteByProductId(pmsProduct.getId());
        if (CollectionUtils.isNotEmpty(pmsProduct.getFullReductionList())) {
            productFullReductionDao.batchInsert(pmsProduct.getFullReductionList());
        }

        skuStockDao.deleteByProductId(pmsProduct.getId());
        if (CollectionUtils.isNotEmpty(pmsProduct.getSkuStockList())) {
            skuStockDao.batchInsert(pmsProduct.getSkuStockList());
        }

        pmsProductAttributeValueDao.deleteByProductId(pmsProduct.getId());
        if (CollectionUtils.isNotEmpty(pmsProduct.getProductAttributeValueList())) {
            pmsProductAttributeValueDao.batchInsert(pmsProduct.getProductAttributeValueList());
        }

        return super.modify(pmsProduct);
    }
}